<?php
namespace app\common\library\helper;

use Yansongda\Pay\Pay;

class PayHelper{

    /**
     * @param $total_fee            //金额（单位是分）
     * @param $openid               //用户openid
     * @param $out_trade_no         //订单号
     * @param string $body          //订单信息
     * @param $place_type           //渠道
     * @return array
     * @desc 微信公众号支付
     */
    public static function wxMp( $total_fee, $openid, $out_trade_no, $body, $place_type ){

        $config = self::_wxConfig( $place_type );

        //支付订单信息
        $order = [
            'out_trade_no' => $out_trade_no,
            'total_fee'    => $total_fee, // **单位：分**
            'body'         => $body,
            'openid'       => $openid,
        ];

        $res = Pay::wechat( $config )->mp( $order );

        return $res->toArray();

    }

    //微信验签
    public static function wxVerify( $place_type ){

        try{

            $config = self::_wxConfig( $place_type );

            $wechat = Pay::wechat( $config );

            $data = $wechat->verify()->toArray();

            $wechat->success()->send();

            return $data;

        }catch( \Exception $e ){

            Helper::logError( $e );

        }

        return false;

    }

    //微信配置
    private static function _wxConfig( $place_type ){

        $wx_config = ConfigHelper::getWxConfig( $place_type );

        //支付配置
        $config = [
            'app_id'     => $wx_config[ 'app_id' ],       //公众号 APPID
            'mch_id'     => $wx_config[ 'mch_id' ],       //商户号
            'key'        => $wx_config[ 'key' ],          //商户支付密码
            'notify_url' => url( 'other/notify/cardwxpay', '', true, true ),        //回调地址 app\other\controller\cardWxpay
            'http'       => [ // optional
                'timeout' => 5.0,
                'connect_timeout' => 5.0,
            ],
        ];

        return $config;

    }

}